#include "PNGTexture.h"

namespace lib3dw
{
	PNGTexture::PNGTexture(std::string textureName, const char* textureFile)
	{
		name = textureName;
		texture = new GLuint[1];
		
		if(textureFile != NULL)
		{
			result = LoadPng(textureFile, &image, width, height);
			glGenTextures(1, &texture[0]);
			glBindTexture(GL_TEXTURE_2D,texture[0]);
			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
			GLsizei w = (GLsizei)this->width;
			GLsizei h = (GLsizei)this->height;
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, this->image);
			free(image);
		}
	}

	PNGTexture::~PNGTexture()
	{
		delete [] texture;
	}
	
	const GLuint PNGTexture::GetId() const
	{
		return texture[0];
	}
	
	const std::string PNGTexture::GetName() const
	{
		return name;
	}
}